//
//  HHPlayerControlView.m
//  HHAVPlayer
//
//  Created by lch on 2022/1/19.
//

#import "HHPlayerControlView.h"
#import <Masonry/Masonry.h>
#import <HHBlockKit/HHBlockHeader.h>
#import "HHAVPlayerHeader.h"

@interface HHPlayerControlView ()

@end

@implementation HHPlayerControlView

- (instancetype)init{
    if (self = [super init]) {
        [self initUI];
    }
    return self;
}
- (void)initUI{
    [self addSubview:self.topImageView];
    [self addSubview:self.bottomImageView];
    
    [self.topImageView addSubview:self.downLoadBtn];
    [self.topImageView addSubview:self.titleLabel];
    [self.topImageView addSubview:self.resolutionBtn];
    [self.bottomImageView addSubview:self.startBtn];
    [self.bottomImageView addSubview:self.currentTimeLabel];
    [self.bottomImageView addSubview:self.progressView];
    [self.bottomImageView addSubview:self.videoSlider];
    [self.bottomImageView addSubview:self.totalTimeLabel];
    [self.bottomImageView addSubview:self.fullScreenBtn];
    
    [self addSubview:self.backBtn];
    [self addSubview:self.lockBtn];
    [self addSubview:self.activity];
    [self addSubview:self.repeatBtn];
    [self addSubview:self.horizontalLabel];
    
    
    UITapGestureRecognizer *sliderTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapSliderAction:)];
    [self.videoSlider addGestureRecognizer:sliderTap];
    // 添加子控件的约束
    [self makeSubViewsConstraints];
    
    [self.activity stopAnimating];
    self.downLoadBtn.hidden     = YES;
    self.resolutionBtn.hidden   = YES;
    // 初始化时重置controlView
    [self resetControlView];
    
}
- (void)makeSubViewsConstraints{
    [self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.mas_leading).offset(15);
        make.top.equalTo(self.mas_top).offset(5);
        make.width.height.mas_equalTo(30);
    }];
    
    [self.topImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.equalTo(self);
        make.height.mas_equalTo(50);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.offset(0);
    }];
    [self.downLoadBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(40);
        make.trailing.equalTo(self.topImageView.mas_trailing).offset(-10);
        make.centerY.equalTo(self.backBtn.mas_centerY);
    }];

    [self.resolutionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(30);
        make.trailing.equalTo(self.downLoadBtn.mas_leading).offset(-10);
        make.centerY.equalTo(self.backBtn.mas_centerY);
    }];
    
    [self.bottomImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.equalTo(self);
        make.height.mas_equalTo(50);
    }];
    
    [self.startBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.bottomImageView.mas_leading).offset(5);
        make.bottom.equalTo(self.bottomImageView.mas_bottom).offset(-5);
        make.width.height.mas_equalTo(30);
    }];
    
    [self.currentTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.startBtn.mas_trailing).offset(-3);
        make.centerY.equalTo(self.startBtn.mas_centerY);
        make.width.mas_equalTo(43);
    }];
    
    [self.fullScreenBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(30);
        make.trailing.equalTo(self.bottomImageView.mas_trailing).offset(-5);
        make.centerY.equalTo(self.startBtn.mas_centerY);
    }];
    
    [self.totalTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.fullScreenBtn.mas_leading).offset(3);
        make.centerY.equalTo(self.startBtn.mas_centerY);
        make.width.mas_equalTo(43);
    }];
    
    [self.progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.currentTimeLabel.mas_trailing).offset(4);
        make.trailing.equalTo(self.totalTimeLabel.mas_leading).offset(-4);
        make.centerY.equalTo(self.startBtn.mas_centerY);
    }];
    
    [self.videoSlider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.currentTimeLabel.mas_trailing).offset(4);
        make.trailing.equalTo(self.totalTimeLabel.mas_leading).offset(-4);
        make.centerY.equalTo(self.currentTimeLabel.mas_centerY).offset(-1);
        make.height.mas_equalTo(30);
    }];
    
    [self.lockBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.mas_leading).offset(15);
        make.centerY.equalTo(self.mas_centerY);
        make.width.height.mas_equalTo(40);
    }];
    
    [self.horizontalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(160);
        make.height.mas_equalTo(40);
        make.center.equalTo(self);
    }];
    
    [self.activity mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
    }];
    
    [self.repeatBtn mas_makeConstraints:^(MASConstraintMaker *make) {
         make.center.equalTo(self);
    }];
    
}

- (void)tapSliderAction:(UITapGestureRecognizer *)tap
{
    if ([tap.view isKindOfClass:[UISlider class]] && self.tapBlock) {
        UISlider *slider = (UISlider *)tap.view;
        CGPoint point = [tap locationInView:slider];
        CGFloat length = slider.frame.size.width;
        // 视频跳转的value
        CGFloat tapValue = point.x / length;
        self.tapBlock(tapValue);
    }
}
/** 重置ControlView */
- (void)resetControlView
{
    self.videoSlider.value      = 0;
    self.progressView.progress  = 0;
    self.currentTimeLabel.text  = @"00:00";
    self.totalTimeLabel.text    = @"00:00";
    self.horizontalLabel.hidden = YES;
    self.repeatBtn.hidden       = YES;
    self.resolutionView.hidden  = YES;
    self.backgroundColor        = [UIColor clearColor];
    self.downLoadBtn.enabled    = YES;
}

/** 切换分辨率时候调用此方法*/
- (void)resetControlViewForResolution{
    self.horizontalLabel.hidden = YES;
    self.repeatBtn.hidden       = YES;
    self.resolutionView.hidden  = YES;
    self.downLoadBtn.enabled    = YES;
    self.backgroundColor        = [UIColor clearColor];
}
/** 显示top、bottom、lockBtn*/
- (void)showControlView{
    self.topImageView.alpha    = 1;
    self.bottomImageView.alpha = 1;
    self.lockBtn.alpha         = 1;
}
/** 隐藏top、bottom、lockBtn*/
- (void)hideControlView{
    self.topImageView.alpha    = 0;
    self.bottomImageView.alpha = 0;
    self.lockBtn.alpha         = 0;
    // 隐藏resolutionView
    self.resolutionBtn.selected = YES;
    [self resolutionAction:self.resolutionBtn];
}
/**
 *  点击topImageView上的按钮
 */
- (void)resolutionAction:(UIButton *)sender
{
    sender.selected = !sender.selected;
    // 显示隐藏分辨率View
    self.resolutionView.hidden = !sender.isSelected;
}

#pragma mark - 属性 setter 方法
- (void)setResolutionArray:(NSArray *)resolutionArray
{
    _resolutionArray = resolutionArray;
    _resolutionBtn.hh_textNormal(resolutionArray.firstObject);
    // 分辨率按钮和分辨率下拉列表
    [self addSubview:self.resolutionView];
    self.resolutionView.hidden = YES;
    
    [self.resolutionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(30*self.resolutionArray.count);
        make.leading.equalTo(self.resolutionBtn.mas_leading).offset(0);
        make.top.equalTo(self.resolutionBtn.mas_bottom).offset(0);
    }];
    if (self.resolutionView.subviews.count > 0) {
        [self.resolutionView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [obj removeFromSuperview];
        }];
    }
    
    // 分辨率View上边的Btn
    for (int i = 0 ; i < resolutionArray.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = 200+i;
        [self.resolutionView addSubview:btn];
        btn.hh_systemFont(14).hh_frame(CGRectMake(0, 30*i, 40, 30)).hh_textNormal(resolutionArray[i]);
        [btn setTitle:resolutionArray[i] forState:UIControlStateNormal];
        [btn hh_buttonClicked:^(UIButton *__weak  _Nonnull button) {
            self.resolutionView.hidden  = YES;
            // 分辨率Btn改为normal状态
            self.resolutionBtn.selected = NO;
            // topImageView上的按钮的文字
            [self.resolutionBtn setTitle:button.titleLabel.text forState:UIControlStateNormal];
            if (self.resolutionBlock) { self.resolutionBlock(button); }
        }];
    }
}

#pragma mark - 初始化
- (UIImageView *)topImageView{
    if (!_topImageView) {
        _topImageView = [[UIImageView alloc]init];
        _topImageView.hh_userInteractionEnabled(YES);
        _topImageView.image = HHPlayerImageName(@"top_shadow");
    }
    return _topImageView;
}

- (UIButton *)backBtn{
    if (!_backBtn) {
        _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backBtn setImage:HHPlayerImageName(@"play_back_full") forState:UIControlStateNormal];
    }
    return _backBtn;
}
- (UIButton *)lockBtn{
    if (!_lockBtn) {
        _lockBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_lockBtn setImage:HHPlayerImageName(@"unlock-nor") forState:UIControlStateNormal];
        [_lockBtn setImage:HHPlayerImageName(@"lock-nor") forState:UIControlStateSelected];
    }
    return _lockBtn;
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.hh_textColor(UIColor.whiteColor)
                .hh_boldSystemFont(16)
                .hh_textAlignment(NSTextAlignmentCenter);
    }
    return _titleLabel;
}
- (UIImageView *)bottomImageView{
    if (!_bottomImageView) {
        _bottomImageView = [[UIImageView alloc]init];
        _bottomImageView.hh_userInteractionEnabled(YES);
        _bottomImageView.image = HHPlayerImageName(@"bottom_shadow");
    }
    return _bottomImageView;
}
- (UIButton *)startBtn{
    if (!_startBtn) {
        _startBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_startBtn setImage:HHPlayerImageName(@"kr-video-player-play") forState:UIControlStateNormal];
        [_startBtn setImage:HHPlayerImageName(@"kr-video-player-pause") forState:UIControlStateSelected];
        
    }
    return _startBtn;
}
- (UILabel *)currentTimeLabel{
    if (!_currentTimeLabel) {
        _currentTimeLabel = [[UILabel alloc]init];
        _currentTimeLabel.hh_textColor(UIColor.whiteColor)
                        .hh_systemFont(12)
                        .hh_textAlignment(NSTextAlignmentCenter);
    }
    return _currentTimeLabel;
}
- (UIProgressView *)progressView{
    if (!_progressView) {
        _progressView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
        _progressView.progressTintColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.5];
        _progressView.trackTintColor    = [UIColor clearColor];
    }
    return _progressView;
}
- (UISlider *)videoSlider
{
    if (!_videoSlider) {
        _videoSlider = [[UISlider alloc] init];
        // 设置slider
        [_videoSlider setThumbImage:HHPlayerImageName(@"slider") forState:UIControlStateNormal];
        _videoSlider.maximumValue          = 1;
        _videoSlider.minimumTrackTintColor = [UIColor whiteColor];
        _videoSlider.maximumTrackTintColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.5];
    }
    return _videoSlider;
}

- (UILabel *)totalTimeLabel
{
    if (!_totalTimeLabel) {
        _totalTimeLabel               = [[UILabel alloc] init];
        _totalTimeLabel.hh_textColor(UIColor.whiteColor)
                    .hh_systemFont(12)
                    .hh_textAlignment(NSTextAlignmentCenter);
        
    }
    return _totalTimeLabel;
}
- (UIButton *)fullScreenBtn{
    if (!_fullScreenBtn) {
        _fullScreenBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_fullScreenBtn setImage:HHPlayerImageName(@"kr-video-player-fullscreen") forState:UIControlStateNormal];
        [_fullScreenBtn setImage:HHPlayerImageName(@"kr-video-player-shrinkscreen") forState:UIControlStateSelected];
    }
    return _fullScreenBtn;
}

- (UILabel *)horizontalLabel{
    if (!_horizontalLabel) {
        _horizontalLabel = [[UILabel alloc]init];
        _horizontalLabel.hh_textColor(UIColor.whiteColor)
            .hh_textAlignment(NSTextAlignmentCenter);
        UIImage *image = [UIImage imageNamed:@"Management_Mask"];
        // 背景色
        _horizontalLabel.layer.contents = (id)image.CGImage;
    }
    return _horizontalLabel;
}
- (UIActivityIndicatorView *)activity
{
    if (!_activity) {
        _activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    }
    return _activity;
}

- (UIButton *)repeatBtn
{
    if (!_repeatBtn) {
        _repeatBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_repeatBtn setImage:HHPlayerImageName(@"repeat_video") forState:UIControlStateNormal];;
    }
    return _repeatBtn;
}

- (UIButton *)downLoadBtn
{
    if (!_downLoadBtn) {
        _downLoadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_downLoadBtn setImage:HHPlayerImageName(@"player_downLoad") forState:UIControlStateNormal];
        [_downLoadBtn setImage:HHPlayerImageName(@"player_not_downLoad") forState:UIControlStateSelected];
    }
    return _downLoadBtn;
}

- (UIButton *)resolutionBtn
{
    if (!_resolutionBtn) {
        _resolutionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _resolutionBtn.hh_systemFont(14)
                    .hh_backgroundColor(RGBA(0, 0, 0, 0.7));
        [_resolutionBtn hh_buttonClicked:^(UIButton *__weak  _Nonnull button) {
            [self resolutionAction:button];
        }];
    }
    return _resolutionBtn;
}
- (UIView *)resolutionView{
    if (!_resolutionView) {
        _resolutionView = [[UIView alloc] init];
        _resolutionView.hidden = YES;
        _resolutionView.backgroundColor = RGBA(0, 0, 0, 0.7);
    }
    return _resolutionView;
}


@end
